<?php
class tbl_character extends AppModel
{
    public $useTable = 'tbl_character';
    public $useDbConfig = 'tales_of_deonia';
    public $primaryKey = 'char_serial';

    public function get_character($_id)
    {
        $params = array(
            'conditions' => array(
                'a2x_id' => $_id,
            ),
            'fields' => array(
                'a2x_id', 'char_serial', 'char_name', 'char_type', 'level', 'gold',
                'energy', 'last_save_date', 'exp'
            ),
        );

        $data = $this->find('all', $params);

        foreach($data as $num => $info):
            switch($info['tbl_character']['char_type'])
            {
                case CHARACTER_SWORD_MAN:
                    $data[$num]['tbl_character']['char_type'] = __(LANG_CHAR_TYPE_SWORDMAN);
                    break;
                case CHARACTER_THIEF:
                    $data[$num]['tbl_character']['char_type'] = __(LANG_CHAR_TYPE_THIEF);
                    break;
                case CHARACTER_MAGICIAN:
                    $data[$num]['tbl_character']['char_type'] = __(LANG_CHAR_TYPE_MAGICIAN);
                    break;
                case CHARACTER_PRIEST:
                    $data[$num]['tbl_character']['char_type'] = __(LANG_CHAR_TYPE_PRIEST);
                    break;
                default :
                    break;
            }
        endforeach;

        return $data;
    }

    public function get_character_select($_id, $_char_serial)
    {
        $params = array(
            'conditions' => array(
                'a2x_id' => $_id,
                'char_serial' => $_char_serial,
            ),
            'fields' => array(
                'a2x_id', 'char_serial', 'char_name', 'char_type', 'level', 'gold',
                'energy', 'last_save_date', 'exp'
            ),
        );

        $data = $this->find('first', $params);

        switch($data['tbl_character']['char_type'])
        {
            case CHARACTER_SWORD_MAN:
                $data['tbl_character']['char_type'] = __(LANG_CHAR_TYPE_SWORDMAN);
                break;
            case CHARACTER_THIEF:
                $data['tbl_character']['char_type'] = __(LANG_CHAR_TYPE_THIEF);
                break;
            case CHARACTER_MAGICIAN:
                $data['tbl_character']['char_type'] = __(LANG_CHAR_TYPE_MAGICIAN);
                break;
            case CHARACTER_PRIEST:
                $data['tbl_character']['char_type'] = __(LANG_CHAR_TYPE_PRIEST);
                break;
            default :
                break;
        }

        return $data;
    }

    public function get_char_level()
    {
        $params = array(
            'fields' => array('count(level) as count', 'level'),
            'group' => 'level'
        );

        $data = $this->find('all', $params);
        return $data;
    }

    public function get_char_total()
    {
        $data = $this->find('count');
        return $data;
    }
}